﻿Imports System.Collections.Generic
Imports System.Data
Imports Biz
Imports System.Data.SqlClient


''' <summary>
''' Clase de la capa de acceso a datos para el manejo de la entidad Departamento
''' </summary>
''' <remarks></remarks>
Public Class DALDepartamento

    Private Shared m_Instance As DALDepartamento
    Private Shared m_Semaforo As New Object
    Private m_TDGDepartamento As TDGDepartamento


    Public Function Insert(obj As cDepartamento) As Boolean
        Dim iResultado As Int16 = 0
        Dim database As cDatabase

        Try
            database = cDatabase.Instance

            database.Conectar()
            iResultado = m_TDGDepartamento.Insert(database.Conexion, obj.Dep_Descrip, obj.DVH, Dep_Codigo:=0)
            database.Desconectar()

            Return (iResultado > 0)
        Catch ex As Exception
            Throw New Excepciones.DepartamentoFalloAlta(ex)
        End Try

    End Function

    Public Function Update(obj As cDepartamento) As Boolean
        Dim iResultado As Int16 = 0
        Dim database As cDatabase

        Try
            database = cDatabase.Instance

            database.Conectar()
            m_TDGDepartamento.Update(database.Conexion, obj.Dep_Codigo, obj.Dep_Descrip, obj.DVH)
            database.Desconectar()

            Return (iResultado > 0)
        Catch ex As Exception
            Throw New Excepciones.DepartamentoFalloEditar(ex)
        End Try
    End Function

    Public Function Delete(obj As cDepartamento) As Boolean
        Dim iResultado As Int16 = 0
        Dim database As cDatabase

        Try
            database = cDatabase.Instance

            database.Conectar()
            m_TDGDepartamento.Delete(database.Conexion, obj.Dep_Codigo)
            database.Desconectar()

            Return (iResultado > 0)
        Catch ex As Exception
            Throw New Excepciones.DepartamentoFalloBaja(ex)
        End Try

    End Function

    Public Function Sel(obj As cDepartamento) As cDepartamento
        Dim dr As SqlDataReader
        Dim database As cDatabase

        Try
            database = cDatabase.Instance

            database.Conectar()
            dr = m_TDGDepartamento.Find(database.Conexion, obj.Dep_Codigo)

            If dr.HasRows Then

                dr.Read()

                obj.Dep_Codigo = Utils.Convert.ToInt16(dr.GetValue(0))
                obj.Dep_Descrip = Utils.Convert.ToStr(dr.GetValue(1))
                obj.DVH = Utils.Convert.ToInt32(dr.GetValue(2))

                dr.Close()
            Else
                obj = Nothing
            End If

            database.Desconectar()

            Return obj
        Catch ex As Exception
            Throw New Excepciones.DepartamentoFalloBuscar(ex)
        End Try
    End Function

    Public Function SelectAll() As Collection
        Dim dr As SqlDataReader
        Dim colObjetos As Collection
        Dim objAux As cDepartamento
        Dim database As cDatabase


        Try
            database = cDatabase.Instance

            database.Conectar()
            dr = m_TDGDepartamento.FindAll(database.Conexion)

            If dr.HasRows Then

                colObjetos = New Collection

                Do While dr.Read

                    objAux = New cDepartamento

                    objAux.Dep_Codigo = Utils.Convert.ToInt16(dr.GetValue(0))
                    objAux.Dep_Descrip = Utils.Convert.ToStr(dr.GetValue(1))
                    objAux.DVH = Utils.Convert.ToInt32(dr.GetValue(2))

            colObjetos.Add(objAux)

                Loop

                dr.Close()

            Else

                colObjetos = Nothing

            End If

            database.Desconectar()

            Return colObjetos
        Catch ex As Exception
            Throw New Excepciones.DepartamentoFalloConsultar(ex)
        End Try
    End Function

    Public Shared Function Instance() As DALDepartamento
        If m_Instance Is Nothing Then
            SyncLock (m_Semaforo)
                If m_Instance Is Nothing Then
                    m_Instance = New DALDepartamento
                End If
            End SyncLock
        End If

        Return m_Instance
    End Function

    Private Sub New()
        m_TDGDepartamento = TDGDepartamento.Instance()
    End Sub

End Class
